/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
    $('#register_form').submit(function(){
        var recaptcha_challenge_field = $('input[name="recaptcha_challenge_field"]').val();
        var recaptcha_response_field = $('input[name="recaptcha_response_field"]').val();
        $.ajax({
            type: "post",
            url: "RegisterController",
            data: "Action=checkCaptcha&recaptcha_challenge_field=" + recaptcha_challenge_field + "&recaptcha_response_field=" + recaptcha_response_field,
            success: function(rs){
                if(rs=="OK"){
                    return true;
                }else{
                    alert("Your Captcha not correct.");
                    return false;
                }
            }
        });
        
    });
   
    // change images when click to thumb
    $("#thumbs_list ul li a").click(function(){
        var src = $('img',this).attr('src');
        $('img#bigpic').attr('src',src);
        return false;
    });

    // check correct information of form
    $('#login_form').submit(function(){
        // validate
        if ($('#email').val() == '')
        {
            // something is wrong
            alert('Please Enter Your Name');
            $('#email').focus();
        }
        else if ($('#passwd').val() =='')
        {
            // something else is wrong
            alert('Please Enter Pasword');
            $('#passwd').focus();
        }

    // close popup and refresh parent
    });
    
    $('#create-account_form').submit(function(){
        // validate
        if ($('#email_create').val() == '')
        {
            // something is wrong
            alert('Please Enter Your Email Address');
            $('#email_create').focus();
        }
    });
    

    // shopping cart beginning....
    var idProduct;
    var sCart=""; // store item selected in cart
    
    // tranfer data in cart to any page..
    if($('#cart_items p').size()!=0){
        $('#cart_items p').each(function(){
            sCart += $(this).attr('id') + ';';
        });
        
    }
    
    
    // when clicked button add to cart
    $('a.add_to_cart_button').click(function(){
        if($.cookie("email")!=null){
        
            idProduct = $(this).attr("title");
            sCart += idProduct + ";";
            $('p#cart_block_no_product').remove();
            $('div#cart_items').append("<p class='cart_item' id='"+ idProduct+"'>"+idProduct+"</p>");
            $.ajax({
                type:"post",
                data: "sCart="+sCart,
                url:"ShoppingCartController",
                success: function(xml){
                    return false;
                }
            });
        }else{
            alert('You must login before add something to cart..');
        }

        
    });
    
    
    // order cart submit
    $('#button_order_cart').click(function(){
        if ( $('div#cart_items').is(':not(:empty)'))  {
           
            $.ajax({
                type: "POST",
                data: "checkout=1",
                url:"ShoppingCartController",
                success: function(msg){
                    msg = jQuery.trim(msg); // remove blank space in msg
                    if(msg == "OK"){
                        $('div#cart_items').empty();
                        $("<p id='cart_block_no_product'>No Products</p>").insertBefore('div#cart_items');
                    
                        alert("Add To Cart Successful.");
                    
                    }else{
                        alert("Add To Cart Fail");
                    }
                }
            });
            return false;
        }else if($.cookie("email")==null){
            alert("Please Login to Checkout.");
            return false;
        }
        else{
            alert ( "Please Add Some Products To Your Cart..." );
            return false;
        }
      
    });
    /*
    $('a#login').click(function(){
        popup();
        return false; // don't open windows login 
    });
 */
    // search handle
    $('a#search').click(function(){
        $(this).parent().submit();
        return false;
    });
     
    $('#searchbox').submit(function(){
        if($('input#search_query_top').val()==''){
            return false;
        }else{
            var idCat = getUrlVars()["idCat"];
            $.ajax({
                type:"POST",
                data:"name="+$('#search_query_top').val() + "&idCat="+idCat,
                url:"finding.jsp",
                success: function(rs){
                    $('#right').html(rs);
                    
                }
            });
        
            return false;
        }
       
    });
    
     
     
     
     
     
     
    // get parameter by jQuery <-- > see more in link http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html;
    $.extend({
        getUrlVars: function(){
            var vars = [], hash;
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for(var i = 0; i < hashes.length; i++)
            {
                hash = hashes[i].split('=');
                
                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }
            
            return vars;
        },
        getUrlVar: function(name){
            //return ($.getUrlVars()['idCat']);
            return $.getUrlVars()[name];
        }
    }); 
    $('input#email').focus(function(){
        $('p#error').fadeOut('slow',function(){
            $(this).css({'display':'none'});
        });
    });
    
     
});

function popup() {
    var w = 750;
    var h = 400;
    var winl = (screen.width-w)/2;
    var wint = (screen.height-h)/2;
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;

    var page = "login.jsp";
    windowprops = "height="+h+",width="+w+",top="+ wint +",left="+ winl +",location=no,"
    + "scrollbars=yes,menubars=no,toolbars=no,resizable=no,status=yes";
    window.open(page, "Popup", windowprops);
}

function refreshParent() {
    window.opener.location.href = window.opener.location.href;

    if (window.opener.progressWindow)
		
    {
        window.opener.progressWindow.close()
    }
    window.close();
}
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}


